/*
* Copyright (c) 2012 Kyle Hayward
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include "Precompiled.h"

#include "Vector.h"
#include "Matrix.h"

namespace OptixDotNet {
namespace Math
{
	void Vector3::Transform( Vector3% vec, Matrix4x4% transform, [Out] Vector3% result )
	{
		result.X  =	transform.X.X * vec.X +
					transform.Y.X * vec.Y +
					transform.Z.X * vec.Z +
					transform.W.X;

		result.Y  =	transform.X.Y * vec.X +
					transform.Y.Y * vec.Y +
					transform.Z.Y * vec.Z +
					transform.W.Y;

		result.Z  =	transform.X.Z * vec.X +
					transform.Y.Z * vec.Y +
					transform.Z.Z * vec.Z +
					transform.W.Z;
	}

	Vector3 Vector3::Transform( Vector3% vec, Matrix4x4% transform )
	{
		float x  =	transform.X.X * vec.X +
					transform.Y.X * vec.Y +
					transform.Z.X * vec.Z +
					transform.W.X;

		float y  =	transform.X.Y * vec.X +
					transform.Y.Y * vec.Y +
					transform.Z.Y * vec.Z +
					transform.W.Y;

		float z  =	transform.X.Z * vec.X +
					transform.Y.Z * vec.Y +
					transform.Z.Z * vec.Z +
					transform.W.Z;

		return Vector3( x, y, z );
	}

	void Vector3::Transform( array<Vector3>^ vectorArray, Matrix4x4% transform )
	{
		for( int i = 0; i < vectorArray->Length; i++ )
		{
			Vector3::Transform( vectorArray[i], transform, vectorArray[i] );
		}
	}

	void Vector3::Transform( Matrix4x4 transform )
	{
		float x =	transform.X.X * X +
					transform.Y.X * Y +
					transform.Z.X * Z +
					transform.W.X;

		float y =	transform.X.Y * X +
					transform.Y.Y * Y +
					transform.Z.Y * Z +
					transform.W.Y;

		float z =	transform.X.Z * X +
					transform.Y.Z * Y +
					transform.Z.Z * Z +
					transform.W.Z;

		X = x;
		Y = y;
		Z = z;
	}

	void Vector3::TransformNormal( Vector3% vec, Matrix4x4% transform, [Out] Vector3% result )
	{
		result.X  =	transform.X.X * vec.X +
					transform.Y.X * vec.Y +
					transform.Z.X * vec.Z;

		result.Y  =	transform.X.Y * vec.X +
					transform.Y.Y * vec.Y +
					transform.Z.Y * vec.Z;

		result.Z  =	transform.X.Z * vec.X +
					transform.Y.Z * vec.Y +
					transform.Z.Z * vec.Z;
	}

	Vector3 Vector3::TransformNormal( Vector3% vec, Matrix4x4% transform )
	{
		float x  =	transform.X.X * vec.X +
					transform.Y.X * vec.Y +
					transform.Z.X * vec.Z;

		float y  =	transform.X.Y * vec.X +
					transform.Y.Y * vec.Y +
					transform.Z.Y * vec.Z;

		float z =	transform.X.Z * vec.X +
					transform.Y.Z * vec.Y +
					transform.Z.Z * vec.Z;

		return Vector3( x, y, z );
	}

	void Vector3::TransformNormal( array<Vector3>^ vectorArray, Matrix4x4% transform )
	{
		for( int i = 0; i < vectorArray->Length; i++ )
		{
			Vector3::TransformNormal( vectorArray[i], transform, vectorArray[i] );
		}
	}

	void Vector3::TransformNormal( Matrix4x4 transform )
	{
		float x  =	transform.X.X * X +
					transform.Y.X * Y +
					transform.Z.X * Z;
		
		float y  =	transform.X.Y * X +
					transform.Y.Y * Y +
					transform.Z.Y * Z;
		
		float z  =	transform.X.Z * X +
					transform.Y.Z * Y +
					transform.Z.Z * Z;

		X = x;
		Y = y;
		Z = z;
	}

	void Vector3::TransformCoord( Vector3% vec, Matrix4x4% transform, [Out] Vector3% result )
	{
		result.X  =	transform.X.X * vec.X +
					transform.Y.X * vec.Y +
					transform.Z.X * vec.Z +
					transform.W.X;

		result.Y  =	transform.X.Y * vec.X +
					transform.Y.Y * vec.Y +
					transform.Z.Y * vec.Z +
					transform.W.Y;

		result.Z  =	transform.X.Z * vec.X +
					transform.Y.Z * vec.Y +
					transform.Z.Z * vec.Z +
					transform.W.Z;

		float w = 1.0f / (( ( (vec.X * transform.X.W) + (vec.Y * transform.Y.W)) + (vec.Z * transform.Z.W) ) + transform.W.W);

		result.X *= w;
		result.Y *= w;
		result.Z *= w;
	}

	Vector3 Vector3::TransformCoord( Vector3% vec, Matrix4x4% transform )
	{
		float x  =	transform.X.X * vec.X +
					transform.Y.X * vec.Y +
					transform.Z.X * vec.Z +
					transform.W.X;
		
		float y  =	transform.X.Y * vec.X +
					transform.Y.Y * vec.Y +
					transform.Z.Y * vec.Z +
					transform.W.Y;
		
		float z  =	transform.X.Z * vec.X +
					transform.Y.Z * vec.Y +
					transform.Z.Z * vec.Z +
					transform.W.Z;

		float w = 1.0f / (( ( (vec.X * transform.X.W) + (vec.Y * transform.Y.W)) + (vec.Z * transform.Z.W) ) + transform.W.W);

		return Vector3( x * w, y * w, z * w );
	}

	void Vector3::TransformCoord( array<Vector3>^ vectorArray, Matrix4x4% transform )
	{
		for( int i = 0; i < vectorArray->Length; i++ )
		{
			Vector3::TransformCoord( vectorArray[i], transform, vectorArray[i] );
		}
	}

	void Vector3::TransformCoord( Matrix4x4 transform )
	{
		float x  =	transform.X.X * X +
					transform.Y.X * Y +
					transform.Z.X * Z +
					transform.W.X;
		
		float y  =	transform.X.Y * X +
					transform.Y.Y * Y +
					transform.Z.Y * Z +
					transform.W.Y;
		
		float z  =	transform.X.Z * X +
					transform.Y.Z * Y +
					transform.Z.Z * Z +
					transform.W.Z;

		float w = 1.0f / (( ( (X * transform.X.W) + (Y * transform.Y.W)) + (Z * transform.Z.W) ) + transform.W.W);

		X = x * w;
		Y = y * w;
		Z = z * w;
	}
}
}